//
//  MYXVistorView.swift
//  weibo
//
//  Created by MaYingxin on 16/4/17.
//  Copyright © 2016年 MaYingxin. All rights reserved.
//

import UIKit
import SnapKit

class MYXVistorView: UIView {

    var vistorClosure: (() -> ())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setVistorInfo(imgName: String?, title: String?) -> () {
        
        if let img = imgName, tit = title {
            
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            
            circleImageView.hidden = true
        }else{
            
            setCirCleAnim()
        }
    }
    
    
    func setCirCleAnim() -> () {
        
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        
        ani.duration = 15
        
        ani.toValue = 2 * M_PI
        
        ani.repeatCount = MAXFLOAT
        
        ani.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(ani, forKey: nil)
        
    }
    
    @objc private func loginClick(){
        
        vistorClosure?()
    }
    
    func setUI() -> () {
        
        self.backgroundColor = RGB(237, green: 237, blue: 237)
        
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        circleImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
            make.size.equalTo(CGSize(width: 230, height: 36))
        }
        
        registerButton.snp_makeConstraints { (make) in
            
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 30))
        }
        
        loginButton.snp_makeConstraints { (make) in
            
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
    }
    
    
    private lazy var circleImageView: UIImageView = {
        
        return UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
        
    }()
    
    private lazy var maskImageView: UIImageView = {
        
        return UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))
    }()
    
    private lazy var iconImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var messageLabel: UILabel = {
       
        let msg = UILabel()
        
        msg.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        msg.textColor = UIColor.grayColor()
        msg.numberOfLines = 2
        msg.textAlignment = .Center
        msg.font = UIFont.systemFontOfSize(14)
        
        return msg
    }()
    
    private lazy var registerButton: UIButton = {
       
        let btn = UIButton()
        
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(themeColor, forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return btn
    }()
    
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(themeColor, forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return btn
    }()

}
